/**
 * video sound
 * Copyright (c) 2009 cmiscm.com
 * date of final modification : 2009. 11. 20 
*/
package com.cmiscm.videos.stream
{
	import com.cmiscm.videos.events.VideoSoundEvent;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.media.SoundTransform;
	import flash.net.SharedObject;
	
	public class CMsound extends Sprite
	{
		private var _stage:Stage;
		private var _refer:CMstream;
		
		private var _isMute:Boolean = false;
		private var _isCookie:Boolean;
		private var _curVolume:Number;
		private var _savedVolume:Number;		
		private var _myVolume:SharedObject; // save volume
		
		public function CMsound($stream:CMstream, $first_volume:Number, $cookie:Boolean)
		{
			super();
			setting($stream, $first_volume, $cookie);
		}
		private function setting($stream:CMstream, $first_volume:Number, $cookie:Boolean):void
		{
			_refer = $stream;
			
			_isCookie = $cookie;
			
			if (_isCookie) { // use to sound cookie
				_myVolume = SharedObject.getLocal("savedVolume", "/");				
				if (_myVolume.data.myVolumeSaved == undefined || _myVolume.data.myVolumeSaved == null) {					
					_curVolume = $first_volume;
					_myVolume.data.myVolumeSaved = _curVolume;
					_myVolume.flush();
				} else {
					_curVolume = _myVolume.data.myVolumeSaved;
				}
			} else {
				_curVolume = $first_volume;
			}			
			
			
			_savedVolume = _curVolume;
			
			var sndTransform:SoundTransform = new SoundTransform();
			sndTransform.volume = _curVolume;
			_refer.stream.soundTransform = sndTransform;
		}
		
		/**
		 * init 
		 * 
		 */		
		public function init():void
		{
			if (_curVolume == 0) {
				_isMute = true;				
			} else {
				_isMute = false;
			}
			this.dispatchEvent(new VideoSoundEvent(VideoSoundEvent.CHANGE_VOLUME, _curVolume));
		}
		
		/**
		 * change volume 
		 */
		public function changeVolume($volume:Number):void
		{
			var trans:SoundTransform = new SoundTransform()
			trans.volume = $volume;
			_refer.stream.soundTransform = trans;
			
			setVolume($volume);
		}
		
		/**
		 * setting volume 
		 */
		public function setVolume($volume:Number):void
		{
			_curVolume = $volume;
			
			if (_curVolume == 0) _isMute = true;
			else _isMute = false;	
					
			this.dispatchEvent(new VideoSoundEvent(VideoSoundEvent.CHANGE_VOLUME, _curVolume));
			
			if (_isCookie) saveSoundCookie();
		}
		
		/**
		 * mute 
		 * 
		 */		
		public function mute():void
		{
			if (_isMute) {
				soundOn();
			} else {
				soundOff();
			}
		}
		
		/**
		 * sound on 
		 * 
		 */		
		public function soundOn():void
		{
			if (_isMute) {
				if (_savedVolume == 0) _savedVolume = 0.6;
				setVolume(_savedVolume);
			}
		}
		
		/**
		 * sound off 
		 * 
		 */		
		public function soundOff():void
		{
			if (!_isMute) {				
				_savedVolume = _curVolume;
				setVolume(0);
			}
		}
		
		/**
		 * save sound
		 */		
		private function saveSoundCookie():void
		{
			_myVolume.data.myVolumeSaved = _curVolume;
			_myVolume.flush();
		}
		
	}
}